# =========================================
# Sensitivity Analysis for Deviation Index
# =========================================

import pandas as pd
import numpy as np
from scipy.stats import spearmanr

# -----------------------------------------
# 1. 读取数据
# -----------------------------------------
file_path = "Media_6Metrics_DeviationSummary_new.xlsx"
df = pd.read_excel(file_path)

# -----------------------------------------
# 2. 定义 6 个 deviation 维度
# -----------------------------------------
metrics = [
    "SigmoidDeviation_BalanceIndex",
    "SigmoidDeviation_ImpactIndex",
    "SigmoidDeviation_ToneIndex",
    "SigmoidDeviation_EventCount",
    "JS_Quad",
    "JS_Event"
]

X = df[metrics].copy()

# -----------------------------------------
# 3. 基准版本：等权重 Deviation Index
# -----------------------------------------
df["Deviation_Equal"] = X.mean(axis=1)

# =========================================================
# Sensitivity Analysis 1: Block Weighting
# （连续维度 vs 分类维度，各占 50%）
# =========================================================

continuous_metrics = [
    "SigmoidDeviation_BalanceIndex",
    "SigmoidDeviation_ImpactIndex",
    "SigmoidDeviation_ToneIndex",
    "SigmoidDeviation_EventCount"
]

categorical_metrics = [
    "JS_Quad",
    "JS_Event"
]

df["Deviation_Block"] = (
        0.5 * df[continuous_metrics].mean(axis=1) +
        0.5 * df[categorical_metrics].mean(axis=1)
)

rho_block, p_block = spearmanr(
    df["Deviation_Equal"],
    df["Deviation_Block"]
)

print("Sensitivity 1: Block Weighting")
print(f"Spearman rho = {rho_block:.3f}, p = {p_block:.4f}")
print("-" * 50)

# =========================================================
# Sensitivity Analysis 2: Leave-One-Dimension-Out (LOO)
# =========================================================

loo_results = []

for metric in metrics:
    remaining_metrics = [m for m in metrics if m != metric]

    col_name = f"Deviation_wo_{metric}"
    df[col_name] = df[remaining_metrics].mean(axis=1)

    rho, p = spearmanr(df["Deviation_Equal"], df[col_name])

    loo_results.append({
        "Removed_Dimension": metric,
        "Spearman_rho": round(rho, 3),
        "p_value": round(p, 4)
    })

loo_df = pd.DataFrame(loo_results)

print("Sensitivity 2: Leave-One-Dimension-Out")
print(loo_df)
print("-" * 50)

# =========================================================
# （可选）保存结果到 Excel，方便做 Appendix
# =========================================================

output_path = "Sensitivity_Analysis_Results.xlsx"

with pd.ExcelWriter(output_path, engine="openpyxl") as writer:
    df.to_excel(writer, sheet_name="All_Deviation_Indices", index=False)
    loo_df.to_excel(writer, sheet_name="LOO_Spearman", index=False)

print(f"Results saved to {output_path}")
